package main

import (
	"github.com/gin-gonic/gin"
	"io"
	"net/http"
	"os"
	"time"
)

func log() {
	t := time.Now()
	dir := t.Format("storage/log/2006-01")
	if !PathExists(dir) {
		os.MkdirAll(dir, os.ModePerm)
	}
	file := dir + t.Format("/2006-01-02.log")
	var f *os.File
	if PathExists(file) {
		f, _ = os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_RDWR, os.ModePerm)
	} else {
		f, _ = os.Create(file)
	}
	gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
	r := gin.Default()
	r.GET("/ping", func(context *gin.Context) {
		context.String(http.StatusOK, "pong")
	})
	r.Run(":8080")
}

func PathExists(path string) bool {
	_, err := os.Stat(path)
	if err == nil {
		return true
	}
	if os.IsNotExist(err) {
		return false
	}
	return false
}
